package com.hw.weathershow.service;

import com.hw.weathershow.entity.Weather;
import com.hw.weathershow.factory.ProduceFactory;
import com.hw.weathershow.mapper.WeatherMapper;
import com.hw.weathershow.util.PojoGenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * The type Query weather service.
 *
 * @author Cejay1314
 * @version 1.0
 * @create 2022 /12/4 13:05
 */
@Service
public class QueryWeatherService {

    @Autowired
    private RestTemplate restTemplate;
    /**
     * The Produce factory.
     */
    @Autowired
    ProduceFactory produceFactory;


//    @Scheduled(cron = "* * * * * 0-7")
//    public void demo(){
//        System.out.println("hello");
//    }
    @Scheduled(cron = "0/15 * * * * 0-7")
    private void queryWeatherInfo(){
        String apiURL = "https://restapi.amap.com/v3/weather/weatherInfo?city=510100&key=3303c29ca8378c7c93dcf768bbc50f95";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);

        if (200 == responseEntity.getStatusCodeValue()) {
            WeatherMapper weatherMapper = produceFactory.getMapperFactory().getWeatherMapper();

            Weather weather = queryWeather();
            Weather weather1 = PojoGenUtil.getInstance().getWeather(responseEntity);
            updateWeather();
            if(weather==null){
                weatherMapper.insert(weather1);
            }else {
                if (!cmpWeather(weather, weather1)) {
                    weather1.setId(weather.getId());
                    weatherMapper.updateById(weather1);
                }
            }
        } else {
            System.out.println("天气请求错误");
        }
    }
    private boolean cmpWeather(Weather x,Weather y){
        if(x==null || y==null) return true;
        if(x.getCity().equals(y.getCity())
                && x.getProvince().equals(y.getProvince())
                && x.getHumidity().equals(y.getHumidity())
                && x.getTemperature().equals(y.getTemperature())
                && x.getWindDirection().equals(y.getWindDirection())
                && x.getWindPower().equals(y.getWindPower())
                && x.getWeather().equals(y.getWeather())
        ){
            return true;
        }else {
            return false;
        }
    }
    private void updateWeather(){
        produceFactory.getObserverFactory().getObserver("WeatherObserver").update();
    }

    /**
     * Query weather weather.
     *
     * @return the weather
     */
    public Weather queryWeather(){
        WeatherMapper weatherMapper = produceFactory.getMapperFactory().getWeatherMapper();

        List<Weather> weathers = weatherMapper.selectList(null);
        if(weathers.size()<=0){
            return null;
        }else {
            return weathers.get(0);
        }
    }
}
